/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ec.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.ec.entity.ItemCategory;
import com.thinkgem.jeesite.modules.ec.entity.ItemType;
import com.thinkgem.jeesite.modules.ec.service.ItemCategoryService;
import com.thinkgem.jeesite.modules.ec.service.ItemTypeService;

/**
 * 商品分类Controller
 * @author zkool
 * @version 2016-01-16
 */
@Controller
@RequestMapping(value = "${adminPath}/ec/itemCategory")
public class ItemCategoryController extends BaseController {

	@Autowired
	private ItemCategoryService itemCategoryService;
	@Autowired
	private ItemTypeService itemTypeService;
	
	@ModelAttribute
	public ItemCategory get(@RequestParam(required=false) String id) {
		ItemCategory entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = itemCategoryService.get(id);
		}
		if (entity == null){
			entity = new ItemCategory();
		}
		return entity;
	}
	
	@RequiresPermissions("ec:itemCategory:view")
	@RequestMapping(value = {"list", ""})
	public String list(ItemCategory itemCategory, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<ItemCategory> list = itemCategoryService.findList(itemCategory); 
		for (ItemCategory cate : list) {
			cate.setItemType(itemTypeService.get(cate.getItemType().getId()));
		}
		model.addAttribute("list", list);
		return "modules/ec/itemCategoryList";
	}

	@RequiresPermissions("ec:itemCategory:view")
	@RequestMapping(value = "form")
	public String form(ItemCategory itemCategory, Model model) {
		if (itemCategory.getParent()!=null && StringUtils.isNotBlank(itemCategory.getParent().getId())){
			itemCategory.setParent(itemCategoryService.get(itemCategory.getParent().getId()));
			// 获取排序号，最末节点排序号+30
			if (StringUtils.isBlank(itemCategory.getId())){
				ItemCategory itemCategoryChild = new ItemCategory();
				itemCategoryChild.setParent(new ItemCategory(itemCategory.getParent().getId()));
				List<ItemCategory> list = itemCategoryService.findList(itemCategory); 
				if (list.size() > 0){
					itemCategory.setSort(list.get(list.size()-1).getSort());
					if (itemCategory.getSort() != null){
						itemCategory.setSort(itemCategory.getSort() + 30);
					}
				}
			}
		}
		if (itemCategory.getSort() == null){
			itemCategory.setSort(30);
		}
		model.addAttribute("itemCategory", itemCategory);
		
		//取类型 zkool 2016-01-16
		List<ItemType> itemTypeList = itemTypeService.findList(new ItemType());
		model.addAttribute("itemTypeList", itemTypeList);
		return "modules/ec/itemCategoryForm";
	}

	@RequiresPermissions("ec:itemCategory:edit")
	@RequestMapping(value = "save")
	public String save(ItemCategory itemCategory, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, itemCategory)){
			return form(itemCategory, model);
		}
		itemCategoryService.save(itemCategory);
		addMessage(redirectAttributes, "保存商品分类成功");
		return "redirect:"+Global.getAdminPath()+"/ec/itemCategory/?repage";
	}
	
	@RequiresPermissions("ec:itemCategory:edit")
	@RequestMapping(value = "delete")
	public String delete(ItemCategory itemCategory, RedirectAttributes redirectAttributes) {
		itemCategoryService.delete(itemCategory);
		addMessage(redirectAttributes, "删除商品分类成功");
		return "redirect:"+Global.getAdminPath()+"/ec/itemCategory/?repage";
	}

	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<ItemCategory> list = itemCategoryService.findList(new ItemCategory());
		for (int i=0; i<list.size(); i++){
			ItemCategory e = list.get(i);
			if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", e.getParentId());
				map.put("name", e.getName());
				mapList.add(map);
			}
		}
		return mapList;
	}
	
}